package com.globallogic.qdumper.xml;

import java.io.Writer;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XmlHelper
{
    public static Document toDocument(ResultSet rs) throws ParserConfigurationException,
        SQLException
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();

        Element results = doc.createElement("results");
        doc.appendChild(results);

        ResultSetMetaData rsmd = rs.getMetaData();
        int colCount = rsmd.getColumnCount();

        while ( rs.next() )
        {
            Element row = doc.createElement("row");
            results.appendChild(row);

            for ( int i = 1; i <= colCount; i++ )
            {
                String columnName = rsmd.getColumnName(i).toLowerCase();
                Object columntValue = rs.getObject(i);
                int columnType = rsmd.getColumnType(i);

                String value;
                if ( columntValue != null )
                {
                    switch (columnType)
                    {
                    case Types.INTEGER:
                        value = String.format("%d", columntValue);
                    default:
                        value = columntValue.toString();
                    }
                }
                else
                {
                    value = "";
                }

                row.setAttribute(columnName, value);
                // Element node = doc.createElement( columnName );
                // node.appendChild( doc.createTextNode( value.toString() ) );
                // row.appendChild( node );
            }
        }
        return doc;
    }

    public static void resultSetToXml(ResultSet resultSet, Writer writer) throws Exception
    {
        Document document = toDocument(resultSet);

        TransformerFactory xf = TransformerFactory.newInstance();
        xf.setAttribute("indent-number", new Integer(2));

        Transformer xformer = xf.newTransformer();
        xformer.setOutputProperty(OutputKeys.METHOD, "xml");
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        //Result out = new StreamResult(new OutputStreamWriter(System.out, "UTF-8"));
         Result out = new StreamResult(writer);

        xformer.transform(new DOMSource(document), out);
    }

}
